package com.fortune.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MStoreTransExcution;
import com.fortune.dto.in.mStoreTrans.MStoreTransAListDto;
import com.fortune.dto.in.mStoreTrans.MStoreTransAddDto;
import com.fortune.dto.in.mStoreTrans.MStoreTransEditDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MStoreTransEnum;
import com.fortune.service.MStoreTransService;
import com.fortune.vo.mStoreTrans.MStoreTransInfo;
import com.fortune.vo.mStoreTrans.MstoreTransDetail;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "mStoreTrans", description = "厂商仓库调拨", tags = {"厂商仓库调拨"})
@RestController
@RequestMapping("mStoreTrans")
public class MStoreTransController {
	@Autowired
	private MStoreTransService ms;
	@ApiOperation(value = "调拨单列表", nickname = "mStoreTransList", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
		@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
		@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
		@ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
		@ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<MStoreTransInfo>> list(HttpServletRequest request,@Validated @ApiIgnore MStoreTransAListDto listDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		listDto.setPage((listDto.getPage()-1)*10);
		PagingData<MStoreTransInfo> pagingData = ms.pagingData(mUserInfo, listDto);
		return new MStoreTransExcution<PagingData<MStoreTransInfo>>(pagingData, MStoreTransEnum.SUCCESS);
	}
	
	@ApiOperation(value = "调拨单详情", nickname = "mStoreTransDetail", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="id", value="调拨单id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "detail",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<MstoreTransDetail> detail(HttpServletRequest request,@Validated @NotBlank(message="调拨单id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		MstoreTransDetail detail = ms.mstoreTransDetail(mUserInfo, id);
		if(detail==null)
			return new MStoreTransExcution<MstoreTransDetail>(null, MStoreTransEnum.FALSE);
		return new MStoreTransExcution<MstoreTransDetail>(detail, MStoreTransEnum.SUCCESS);	
	}
	
	@ApiOperation(value = "添加调拨单", nickname = "mStoreTransAdd", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody MStoreTransAddDto addDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		addDto.setCreateUid(mUserInfo.getId());
		int add = ms.add(mUserInfo, addDto);
		if(add==1){
			return new MStoreTransExcution<Void>(null, MStoreTransEnum.SUCCESS);
		}else if (add==2) {
			return new MStoreTransExcution<Void>(null, MStoreTransEnum.LAKING);
		}
		return new MStoreTransExcution<Void>(null, MStoreTransEnum.FALSE);
	}
	
	@ApiOperation(value = "编辑调拨单", nickname = "mStoreTransEdit", notes = "", tags={ "@岳希伟" })
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody MStoreTransEditDto editDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		editDto.setCreateUid(mUserInfo.getId());
		int edite = ms.edit(mUserInfo, editDto);
		if(edite==1)
			return new MStoreTransExcution<Void>(null, MStoreTransEnum.SUCCESS);
		return new MStoreTransExcution<Void>(null, MStoreTransEnum.FALSE);
	}
	
	@ApiOperation(value = "删除调拨单", nickname = "mStoreTransDelete", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="id", value="调拨单id",paramType="query",dataType="string",required=true)
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="调拨单id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		int delete = ms.delete(mUserInfo, id);
		if(delete==1)
			return new MStoreTransExcution<Void>(null, MStoreTransEnum.SUCCESS);
		return new MStoreTransExcution<Void>(null, MStoreTransEnum.FALSE);
	}
	
	@ApiOperation(value = "出库", nickname = "mStoreTransOutConfirm", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="id", value="调拨单id",paramType="query",dataType="string",required=true)
    @PutMapping(value = "outConfirm",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> outConfirm(HttpServletRequest request,@Validated @NotBlank(message="调拨单id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		int result = ms.outConfirm(mUserInfo, id);
		if(result==1){
			return new MStoreTransExcution<Void>(null, MStoreTransEnum.SUCCESS);
		}else if (result==2) {
			return new MStoreTransExcution<Void>(null, MStoreTransEnum.OUTFALSE);
		}else if (result == 3){
			return new MStoreTransExcution<Void>(null, MStoreTransEnum.OUTFALSENO);
		}else if (result == 4){
			return new MStoreTransExcution<Void>(null, MStoreTransEnum.LAKING);
		}
		return new MStoreTransExcution<Void>(null, MStoreTransEnum.FALSE);
	}
	
	@ApiOperation(value = "入库", nickname = "mStoreTransInConfirm", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="id", value="调拨单id",paramType="query",dataType="string",required=true)
    @PutMapping(value = "inConfirm",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> inConfirm(HttpServletRequest request,@Validated @NotBlank(message="调拨单id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		int result = ms.inConfirm(mUserInfo, id);
		if(result==1)
			return new MStoreTransExcution<Void>(null, MStoreTransEnum.SUCCESS);
		return new MStoreTransExcution<Void>(null, MStoreTransEnum.FALSE);
	}
}
